package org.ft.domain.repositories;

import com.baomidou.mybatisplus.annotation.SqlParser;
import com.baomidou.mybatisplus.core.mapper.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.ft.domain.entities.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @copyright (C), 2015-2019, XXX有限公司
 * @fileName: ITenantBusRepository
 * @author: 李阳
 * @date: 2019/7/10 13:50
 * @description:
 * @history: <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
@Repository
public interface ITenantBusRepository extends Mapper<Object> {

    /**
     * 获取租户用户信息
     *
     * @param userName
     * @param tenantId
     * @return
     */
    @SqlParser(filter = true)
    @Select("SELECT * FROM sys_user where user_name=#{userName} and tenant_id=#{tenantId} and deleted=0")
    UserEntity selectTenantUser(String userName, Long tenantId);

    /**
     * 获取租户的用户角色关系
     *
     * @param userId
     * @param tenantId
     * @return
     */
    @SqlParser(filter = true)
    @Select("SELECT * FROM sys_user_role where user_id=#{userId} and tenant_id=#{tenantId} and deleted=0")
    List<UserRoleEntity> selectTenantUserRoles(Long userId, Long tenantId);

    /**
     * 获取租户的用户角色信息
     *
     * @param roleIds
     * @param tenantId
     * @return
     */
    @SqlParser(filter = true)
    @Select({
            "<script>",
            "select * from sys_role where tenant_id=#{tenantId} and deleted=0 and id in",
            "<foreach collection='roleIds' item='roleId' open='(' separator=',' close=')'>",
            "#{roleId}",
            "</foreach>",
            "</script>"
    })
    List<RoleEntity> selectTenantRoles(List<Long> roleIds, Long tenantId);

    /**
     * 获取租户角色权限关系
     *
     * @param roleIds
     * @return
     */
    @SqlParser(filter = true)
    @Select({
            "<script>",
            "select * from sys_role_permission where tenant_id=#{tenantId} and deleted=0 and role_id in",
            "<foreach collection='roleIds' item='roleId' open='(' separator=',' close=')'>",
            "#{roleId}",
            "</foreach>",
            "</script>"
    })
    List<RolePermissionEntity> selectTenantRolePermissions(List<Long> roleIds, Long tenantId);

    /**
     * 获取租户权限信息
     *
     * @param pIds
     * @return
     */
    @SqlParser(filter = true)
    @Select({
            "<script>",
            "select * from sys_permission where deleted=0 and id in",
            "<foreach collection='pIds' item='pId' open='(' separator=',' close=')'>",
            "#{pId}",
            "</foreach>",
            "</script>"
    })
    List<PermissionEntity> selectTenantPermissions(@Param("pIds") List<Long> pIds);
}
